package me.jiatao.wan.ui.main.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.hi.dhl.binding.databind
import com.youth.banner.adapter.BannerAdapter
import me.jiatao.wan.R
import me.jiatao.wan.data.network.pojo.BannerPojo
import me.jiatao.wan.databinding.ItemHomeBannerBinding


/**
 * @Author: taojia
 * @CreateDate: 2021年8月26日 00:46:49
 * @Version: 1.0
 * @Description: 首页轮播图适配器
 */

class HomeBannerAdapter(
    bannerList: List<BannerPojo>
) : BannerAdapter<BannerPojo, HomeBannerAdapter.BannerViewHolder>(bannerList) {

    override fun onCreateHolder(parent: ViewGroup?, viewType: Int): BannerViewHolder {
        /**
         * 加载模版View
         */
        val layoutView =
            LayoutInflater.from(parent?.context).inflate(R.layout.item_home_banner, parent, false)
        return BannerViewHolder(layoutView)
    }

    override fun onBindView(
        holder: BannerViewHolder?,
        data: BannerPojo?,
        position: Int,
        size: Int
    ) {
        holder?.bindData(data)
    }

    class BannerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        /**
         * 初始化数据绑定
         */
        private val itemBinding: ItemHomeBannerBinding by databind()

        fun bindData(bannerItem: BannerPojo?) {
            bannerItem?.let {
                itemBinding.apply {
                    /** 完成数据注入 */
                    bannerEntity = it
                    /** 立刻刷新View */
                    executePendingBindings()
                }
            }
        }
    }
}